<script setup lang="tsx">
import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { fetchGetGoodList } from '@/service/api';
import { deleteGood } from '@/service-alova/api/goods';
import { $t } from '@/locales';
import { useAppStore } from '@/store/modules/app';
import { useTable, useTableOperate } from '@/hooks/common/table';
import OrderSearch from './modules/order-search.vue';
import OrderOperateDrawer from './modules/order-operate-drawer.vue';
const appStore = useAppStore();
const {
  columns,
  columnChecks,
  data,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetGoodList,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    goodName: null,
    goodDescription: null,
    goodCategory: null,
    goodStatus: null,
    goodPrice: null
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'index',
      title: $t('common.index'),
      align: 'center',
      width: 48
    },
    {
      key: 'id',
      title: '商品ID',
      align: 'center',
      width: 48
    },
    {
      key: 'goodName',
      title: '商品名字',
      align: 'center',
      minWidth: 48
    },
    {
      key: 'goodDescription',
      title: '商品描述',
      align: 'center',
      minWidth: 80
    },
    {
      key: 'goodCategory',
      title: '商品分类',
      align: 'center',
      width: 50
    },
    {
      key: 'goodPrice',
      title: '价格',
      align: 'center',
      width: 50
    },
    {
      key: 'goodStockQuantity',
      title: '库存',
      align: 'center',
      width: 50
    },
    {
      key: 'goodImgUrl',
      title: '商品图',
      align: 'center',
      width: 120,
      render: row => <img src={`/src/assets/goodimg/${row.goodImgUrl}`} alt={row.goodName} />
    },
    {
      key: 'goodCreatedat',
      title: '创建时间',
      align: 'center',
      width: 80,
      render: row => <span>{row.goodCreatedat}</span>
    },
    {
      key: 'goodUpdatedat',
      title: '更新时间',
      align: 'center',
      width: 80,
      render: row => <span>{row.goodUpdatedat}</span>
    },
    {
      key: 'goodStatus',
      title: '状态',
      align: 'center',
      width: 100,
      render: row => {
        if (row.goodStatus === null) {
          return null;
        }
        const tagMap: Record<Api.SystemManage.GoodStatus, NaiveUI.ThemeColor> = {
          0: 'warning',
          1: 'success'
        };

        return <NTag type={tagMap[0]}>上架</NTag>;
      }
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      width: 160,
      render: row => (
        <div class="flex-center gap-8px">
          <NButton type="primary" ghost size="small" onClick={() => edit(row.id)}>
            {$t('common.edit')}
          </NButton>
          <NPopconfirm onPositiveClick={() => handleDelete(row.id)}>
            {{
              default: () => $t('common.confirmDelete'),
              trigger: () => (
                <NButton type="error" ghost size="small">
                  {$t('common.delete')}
                </NButton>
              )
            }}
          </NPopconfirm>
        </div>
      )
    }
  ]
});

const {
  drawerVisible,
  operateType,
  editingData,
  handleAdd,
  handleEdit,
  checkedRowKeys,
  onBatchDeleted,
  onDeleted
  // closeDrawer
} = useTableOperate(data, getData);

async function handleBatchDelete() {
  // request
  // eslint-disable-next-line no-console
  console.log(checkedRowKeys.value);
  onBatchDeleted();
}
// 删除byID
async function handleDelete(id: number) {
  // request
  // eslint-disable-next-line no-console
  console.log(id);
  await deleteGood(id);
  onDeleted();
}

function edit(id: number) {
  handleEdit(id);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <OrderSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <NCard title="商品列表" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          @add="handleAdd"
          @refresh="getData"
          @delete="handleBatchDelete"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="962"
        :loading="loading"
        remote
        :row-key="row => row.id"
        :pagination="mobilePagination"
        class="sm:h-full"
      />
      <OrderOperateDrawer
        v-model:visible="drawerVisible"
        :operate-type="operateType"
        :row-data="editingData"
        @submitted="getDataByPage"
      />
    </NCard>
  </div>
</template>

<style scoped></style>
